<?php
/**
 * es 扫描，批量写入脚本，索引重建脚本
 */
header("content-type:text/plain;charset=GB2312");
$conf = array(
    'hosts' => '127.0.0.1:9200',
);
reBuildIndex('baidu_xiaoqu', 'baidu_xiaoqu2', $conf);

function reBuildIndex($old_index, $new_index, $conf) {
    $url =  $conf['hosts'];
    // 尝试创建 新索引
    try{
        fly_curl($url . '/' . $new_index, "{}");
        echo "索引 [",$new_index,"] 创建成功！ ", "\n";
    } catch (Exception $e) {
        echo "索引 [",$new_index,"] 已经存在！", "\n";
    }
    echo "准备读取 [",$old_index,"] 索引数据 ...", "\n";
    $post = json_encode(array("size"=>1000,"query"=>array("match_all"=>array())));
    $res = fly_curl($url . '/' . $old_index . '/_search?search_type=scan&scroll=10m',$post) ;
    $res = json_decode($res, true);
    $body = '';
    $count = 0;
    while ($page = fly_curl($url . '/_search/scroll?scroll=10m&&scroll_id=' . $res["_scroll_id"])) {
        $page = json_decode($page, true);
        $count += count($page["hits"]["hits"]);
        echo "读取 [",$old_index,"] 索引数据 ...", "数据读取 ",$count,"/",$page["hits"]["total"]," ... 进度 ",$count/$page["hits"]["total"] * 100,"%","\n";
        foreach ($page["hits"]["hits"] as $item) {
            $body .= '{ "create": { "_index": "'.$new_index.'", "_type": "'.$item['_type'].'", "_id": "'.$item['_id'].'" }}';
            $body .= "\n";
            $body .= json_encode($item["_source"]);
            $body .= "\n";
        }
        $success = substr_count(fly_curl($url.'/_bulk',$body), '"successful"');
        echo "写入 [",$new_index,"] 索引 ...", "目标写入", count($page["hits"]["hits"]),"条数据,","成功写入",$success,"条数据";
    }
}



function fly_curl($url, $postFields = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        // curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
    if (! empty($postFields)) {
        
        $postBodyString = "";
        $postMultipart = false;
        
        if (is_array($postFields) && 0 < count($postFields)) {
            foreach ($postFields as $k => $v) {
                if ("@" != substr($v, 0, 1)) {
                    // 判断是不是文件上传
                    $postBodyString .= "$k=" . urlencode($v) . "&";
                } else {
                    // 文件上传用multipart/form-data，否则用www-form-urlencoded
                    $postMultipart = true;
                }
            }
            unset($k, $v);
        } else {
            $postBodyString = $postFields . "&";
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($postMultipart) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, - 1));
        }
    }
    $reponse = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new Exception(curl_error($ch), 0);
    } else {
        $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (200 !== $httpStatusCode) {throw new Exception($reponse, $httpStatusCode);}
    }
    curl_close($ch);
    return $reponse;
}